package com.glory.dgcpcpda.activity.mfg;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.adapter.ChangeShiftsAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.entity.ChangeShiftEntity;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.PackageDetailListEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

public class ShiftExchangeActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvWorkNo)
    TextView tvWorkNo;
    @BindView(R.id.etShift)
    FreeUI_ClearEditText etShift;
    @BindView(R.id.etComment)
    FreeUI_ClearEditText etComment;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.list_tv1)
    TextView listTv1;
    @BindView(R.id.list_tv2)
    TextView listTv2;
    @BindView(R.id.list_tv3)
    TextView listTv3;
    @BindView(R.id.list_tv4)
    TextView listTv4;
    @BindView(R.id.list_tv5)
    TextView listTv5;
    @BindView(R.id.list_tv6)
    TextView listTv6;
    @BindView(R.id.list_tv7)
    TextView listTv7;
    @BindView(R.id.rvLotInfoList)
    RecyclerView rvLotInfoList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private ChangeShiftsAdapter mChangeShiftsAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_shift_exchange;
    }

    @Override
    protected void initView() {
        initLayout();
        mChangeShiftsAdapter = new ChangeShiftsAdapter(null);
        mChangeShiftsAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mChangeShiftsAdapter.isFirstOnly(true);
        rvLotInfoList.setAdapter(mChangeShiftsAdapter);

        initEdit(etLot, s -> LotUtils.searchLotInfo(ShiftExchangeActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
            @Override
            public void onSuccess(LotInfoEntity response) {

                if (!response.getLOT().getSTATE().equals("正在作业")){
                    showToast("只有正在作业可以交班",false);
                    return;
                }

                for (ChangeShiftEntity entity : mChangeShiftsAdapter.getData()){
                    if (entity.getITEMID().equals(response.getLOT().getLOTID())){
                        showToast("批次已添加到列表",false);
                        return;
                    }
                }
                //重新构建实体类，将接口需要字段传入
                ChangeShiftEntity changeShiftEntity = new ChangeShiftEntity(response.getLOT().getLOTID(),
                        response.getLOT().getMAINQTY(),
                        response.getLOT().getEQUIPMENTID(),
                        response.getLOT().getSTATE());
                changeShiftEntity.setRESERVED05(response.getLOT().getSTEPNAME());
                changeShiftEntity.setRESERVED06(response.getLOT().getSTEPDESC());
                mChangeShiftsAdapter.addData(changeShiftEntity);
                etLot.setText("");
            }

            @Override
            public void onFail(Throwable t) {
                loadError(t,"LOTQUERY");
            }
        }));
    }

    private void initLayout() {
        tvTitle.setText("交班");
        setMainLayout(llMain);
        listTv1.setText("批号");
        listTv2.setText("批次数量");
        listTv3.setText("设备编号");
        listTv4.setText("批次状态");
        listTv5.setText("已完成数量");
        listTv6.setText("剩余数量");
        listTv7.setText("清除");
        listTv5.setVisibility(View.VISIBLE);
        listTv6.setVisibility(View.VISIBLE);
        listTv7.setVisibility(View.VISIBLE);
        initRecyclerView(rvLotInfoList,this);
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.tvBaseSubmit)
    public void onViewClicked() {
        if (actionControl("Submit19")){
            return;
        }

        if(getText(etShift).equals("")){
            showToast("请输入交班人",false);
            return;
        }

        if (mChangeShiftsAdapter.getData().size() <= 0){
            showToast("请添加列表信息后操作",false);
            return;
        }

        for (ChangeShiftEntity entity : mChangeShiftsAdapter.getData()){
            if (entity.getCOMMENTS().equals("") || entity.getCOMMENTS().equals("0")){
                showToast("已完成数量不能为空",false);
                return;
            }

            if (entity.getRESERVED01().equals("") || entity.getRESERVED01().equals("0")){
                showToast("剩余数量不能为空",false);
                return;
            }
        }

        showSubmitDialog(new onSubmitListener() {
            @Override
            public void setOnSubmitListener() {
                FreeUI_EntityCallBack<BaseResponseBean<ChangeShiftEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<ChangeShiftEntity>>
                        (new TypeToken<BaseResponseBean<ChangeShiftEntity>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<ChangeShiftEntity>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                            showConfirmDialog(ShiftExchangeActivity.this,"交班成功，单据号为:"
                                    + response.body().getResponse().getBody().getDOCID());
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<ChangeShiftEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(),"CHANGESHIFT");
                    }
                };

                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE","REFER");
                map.addProperty("REFEROWNER",getText(etShift));
                map.addProperty("COMMENTS",getText(etComment));
                map.add("CHANGESHIFTLINELIST",new JsonParser().parse(new Gson().toJson(mChangeShiftsAdapter.getData())).getAsJsonArray());
                NetUtils.requestPostNet("CPC.CHANGESHIFT","CHANGESHIFT",this,map,callBack);
            }
        });

    }


}
